﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using AuctionSite.App_Code.BLL;
using AuctionSite.App_Code.DAL;
using AuctionSite.App_Code.Modelos;
using System.Data;
using System.IO;


namespace AuctionSite.Usuarios
{
    public partial class CadastrarProduto : System.Web.UI.Page
    {
       
        SqlConnection conn = new SqlConnection(Dados.StringDeConexao);
        
            
        #region BIND COMBO CATEGORIAS
        public void Bind_ddlcat()
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("select cat_id,cat_nome from categorias", conn);
            SqlDataReader dr = cmd.ExecuteReader();
            ddlcat.DataSource = dr;
            ddlcat.Items.Clear();
            ddlcat.Items.Add("--Selecione--");
            ddlcat.DataTextField = "cat_nome";
            ddlcat.DataValueField = "cat_id";
            ddlcat.DataBind();
            conn.Close();
        }
        #endregion 

        #region BIND COMBO SUBCATEGORIAS
        public void Bind_ddlsub()
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("select sub_id,sub_nome from subcategorias where cat_id='" + ddlcat.SelectedValue + "'", conn);
            SqlDataReader dr = cmd.ExecuteReader();
            ddlsub.DataSource = dr;
            ddlsub.Items.Clear();
            ddlsub.Items.Add("--Selecione--");
            ddlsub.DataTextField = "sub_nome";
            ddlsub.DataValueField = "sub_id";
            ddlsub.DataBind();
            conn.Close();
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind_ddlcat();
            }
            
        }
        protected void ddlcat_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind_ddlsub();
        }

        protected void btnCadastrar_Click(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                

                #region Nome | descricao | Data Inicial | Data Final | Categoria | Subcategoria | inicializar String das fotos | Vendedor
                
                string nome = txtNome.Text;
                string desc = txtDesc.Text;
                string data_inicial = txtDataFinal.Text;
                string data_final = txtDataFinal.Text;
                int categoria = ddlcat.SelectedIndex;
                int subcategoria = ddlsub.SelectedIndex;
                string up1 = "";
                string up2 = "";
                string up3 = "";                
                string v = Session["id"].ToString();
                int vendedor = Convert.ToInt32(v);
                string reverso = "nao";                
                #endregion
                
                string valor_inicial = txtValorInicial.Text.Replace(".","");
                valor_inicial = valor_inicial.Replace("R$ ", "");
                
                decimal valor = Convert.ToDecimal(valor_inicial);

                Response.Write(valor.ToString());

                #region Teste de Impressao
                /*
                Response.Write(nome + "<br>");
                Response.Write(desc + "<br>");
                Response.Write(categoria + "<br>");
                Response.Write(subcategoria + "<br>");
                Response.Write("==================");
                Response.Write(Session["id"]+" - ");
                Response.Write(Session["nome"]);
                */
                #endregion
                
                #region UPLOAD de FOTOS

                if (fup1.PostedFile != null) {                    
                    string pasta    = Server.MapPath("./imgs/");
                    Guid nomeUnico  = Guid.NewGuid();
                    string extensao = Path.GetExtension(fup1.FileName);
                    string caminho = pasta + nomeUnico + extensao;
                    fup1.SaveAs(caminho);
                    up1 = caminho;
                }

                #endregion

                #region MODELO => BLL => Incluir BLL

                Produto p = new Produto(nome, desc, categoria, subcategoria, up1, up2, up3, valor, Convert.ToDateTime(data_inicial), Convert.ToDateTime(data_final), vendedor,reverso);
                ProdutoBLL pbll = new ProdutoBLL();
                pbll.Incluir(p);

                Response.Write(
                        "<script type='text/javascript'>" +
                            "alert ('Cadastro Efetuado com Sucesso!');" +
                            "window.location.href='Default.aspx';" +
                         "</script>");
                #endregion

    

            }
        }
        

    }
}